package com.opNaga.bpmp.connectingObject.association;

import com.opNaga.bpmp.BPMPElement;
import com.opNaga.bpmp.connectingObject.ConnectingObject;
import com.opNaga.bpmp.connectingObject.sequenceFlow.SequenceFlowTarget;

/**
 * User: Junyi BEI
 * Date: 19/01/2012
 * Time: 14:01
 */
public class AssociationFlow extends ConnectingObject {
  public AssociationFlow() {
    super();
  }

  public AssociationFlow(String t) {
    super(t);
  }

  @Override
  public String getType() {
    return "Association Flow";
  }

  @Override
  public boolean destroy() {
    if(source != null && !((AssociationFlowSource) source).removeOutwardAssociationFlow(this)) {
      return standardMessage(810, source);
    }
    if(target != null && !((AssociationFlowTarget) target).removeInwardAssociationFlow(this)) {
      return standardMessage(811, target);
    }
    return super.destroy();
  }

  @Override
  public boolean validateTarget(BPMPElement targetElement) {
    if(!SequenceFlowTarget.class.isAssignableFrom(targetElement.getClass())) {
      return standardMessage(350, targetElement);
    }
    if(!((AssociationFlowTarget) targetElement).addInwardAssociationFlow(this)) {
      return standardMessage(354, targetElement);
    }
    return standardMessage(392, targetElement);
  }

  @Override
  public boolean validateSource(BPMPElement sourceElement) {
    if(!AssociationFlowSource.class.isAssignableFrom(sourceElement.getClass())) {
      return standardMessage(360, sourceElement);
    }
    if(!((AssociationFlowSource) sourceElement).addOutwardAssociationFlow(this)) {
      return standardMessage(364, sourceElement);
    }
    return standardMessage(393, sourceElement);
  }
}
